package com.sixmac.utils;

import org.apache.commons.codec.binary.Base64;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Arrays;
import java.util.Random;

//@Service
public class Test implements ApplicationListener<ContextRefreshedEvent> {
// 
//             String encrypt = jsonMsg.getString("encrypt");
//        JSONObject infoJson;
//        if (encrypt != null) {
//            try {
//                String msg = AesUtil.decrypt(encrypt);
//                infoJson = JSON.parseObject(msg);
//            } catch (Exception e) {
//                LOG.error("parse encrypt msg fail, {}", encrypt);
//                return;
//            }
//        } else {
//            infoJson = jsonMsg;
//        }
//
//
//
//
//    /**
//     * 对密文进行解密.
//     *
//     * @param text 需要解密的密文
//     * @return 解密得到的明文
//     * @throws AesException aes解密失败
//     */
    public static String decrypt(String text) throws Exception {
        byte[] original = null;
        try {
        	byte[] AES_KEY = {(byte)0x56,(byte)0xBC,(byte)0x70,(byte)0x68,(byte)0xED,(byte)0x11,(byte)0x91,(byte)0x70,
        	        (byte)0x2C, (byte)0xA2,(byte)0x14,(byte)0x52,(byte)0x53,(byte)0x5E,(byte)0xDD,(byte)0xC8};
        	
        	String aa = "YW9OQUZHSGxuX2tpd2Fnb2FvTkFGR0hsbl9raXdhZ28=";
        	AES_KEY = Base64.decodeBase64(aa.getBytes());
        	
            // 设置解密模式为AES的CBC模式
            Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
            SecretKeySpec key_spec = new SecretKeySpec(AES_KEY, "AES");
            IvParameterSpec iv = new IvParameterSpec(Arrays.copyOfRange(AES_KEY, 0, 16));
            cipher.init(Cipher.DECRYPT_MODE, key_spec, iv);
//
            // 使用BASE64对密文进行解码
            byte[] encrypted = Base64.decodeBase64(text);

            // 解密
            original = cipher.doFinal(encrypted);
        } catch (Exception e) {
            e.printStackTrace();
        }
//
        String content = null;
        String  from_appid=null;
        try {
            // 去除补位字符
            byte[] bytes = PKCS7Encoder.decode(original);
            // 分离16位随机字符串,网络字节序和AppId
            byte[] networkOrder = Arrays.copyOfRange(bytes, 16, 20);
            int length = recoverNetworkBytesOrder(networkOrder);
            content = new String(Arrays.copyOfRange(bytes, 20, 20 + length), "utf-8");
            from_appid = new String(Arrays.copyOfRange(bytes, 20 + length, bytes.length), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
//            throw new Exception(Exception.IllegalBuffer);
        }
//
        return content;
    }

    static int recoverNetworkBytesOrder(byte[] orderBytes) {
        int sourceNumber = 0;
        for (int i = 0; i < 4; i++) {
            sourceNumber <<= 8;
            sourceNumber |= orderBytes[i] & 0xff;
        }
        return sourceNumber;
    }
    
    public static String darwRandString() {
		Random rand = new Random();
		String s = "012345678901234567890123456789012345678901234567890123456789";
		char randStr = s.charAt(rand.nextInt(s.length()));
		return String.valueOf(randStr);
	}
    
	@Override
	public void onApplicationEvent(ContextRefreshedEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
//}
